//
//  SelectCarTypeViewController.m
//  OCUI
//
//  Created by login on 15-7-31.
//
//

#import "SelectCarTypeViewController.h"
#import "SelectCarTypeTableViewCell.h"
#define DEFAULTCOLOR [UIColor colorWithRed:61.0/255.0 green:154.0/255.0 blue:240.0/255.0 alpha:1.0]

@interface SelectCarTypeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *dataArray;
    UITableView *carTypeTableView;
    UITableView *priceTableView;
    UITableView *servieTableView;
    UISegmentedControl *segment;
    NSIndexPath	* lastIndexPath;
    NSIndexPath *segleft;
    NSIndexPath *segcenter;
    NSIndexPath *segright;

    NSMutableArray *_service;

}
@property(nonatomic,copy)NSArray *contentArray;
@property(nonatomic,copy)NSString *carType;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSMutableArray *service;
@end

@implementation SelectCarTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _service = [[NSMutableArray alloc]init];
    
    NSArray *array1 = @[@"舒适性专车",@"经济型专车",@"商务型专车",@"豪华型专车"];
    NSArray *array2 = @[@"200以下",@"200-400",@"400-600",@"600-1000"];
    NSArray *array3 = @[@"某某某",@"北京汽车",@"易道用车",@"西城汽车公司"];
    dataArray = @[array1,array2,array3];
    
    CGRect mainRect = [UIScreen mainScreen].bounds;
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, mainRect.size.width, mainRect.size.height/3 + 30)];
    [self.view addSubview:backView];
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"按车型",@"按价格",@"按服务商",nil];
    
    segment = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segment.frame = CGRectMake(20, 10, mainRect.size.width - 40, 30);
    segment.selectedSegmentIndex = 0;
    self.contentArray = [dataArray[0] copy];
    [segment addTarget:self action:@selector(changeTableView:) forControlEvents:UIControlEventValueChanged];
    [backView addSubview:segment];
    
    carTypeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, mainRect.size.width, mainRect.size.height/3 - 50)];
    carTypeTableView.delegate = self;
    carTypeTableView.dataSource = self;
    carTypeTableView.showsVerticalScrollIndicator = NO;
    carTypeTableView.showsHorizontalScrollIndicator = NO;
    carTypeTableView.hidden = NO;
    [backView addSubview:carTypeTableView];
    
    
    priceTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, mainRect.size.width, mainRect.size.height/3 - 50)];
    priceTableView.delegate = self;
    priceTableView.dataSource = self;
    priceTableView.showsVerticalScrollIndicator = NO;
    priceTableView.showsHorizontalScrollIndicator = NO;
    priceTableView.hidden = YES;
    [backView addSubview:priceTableView];

    
    servieTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, mainRect.size.width, mainRect.size.height/3 - 50)];
    servieTableView.delegate = self;
    servieTableView.dataSource = self;
    servieTableView.showsVerticalScrollIndicator = NO;
    servieTableView.showsHorizontalScrollIndicator = NO;
    servieTableView.hidden = YES;
    [backView addSubview:servieTableView];

    
    UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, mainRect.size.height/3, 50, 30)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(closeSelfWindow) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cancelButton];
    
    UIButton *confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 50, mainRect.size.height/3, 50, 30)];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(closeSelfWindowAndGetData) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:confirmButton];
    
}

- (void)changeTableView:(UISegmentedControl *)seg
{
//    self.contentArray = [dataArray[seg.selectedSegmentIndex] copy];
//    [segmentContent reloadData];
    
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            carTypeTableView.hidden = NO;
            priceTableView.hidden = YES;
            servieTableView.hidden = YES;
            lastIndexPath = [segleft copy];
        }
            break;
        case 1:
        {
            carTypeTableView.hidden = YES;
            priceTableView.hidden = NO;
            servieTableView.hidden = YES;
            lastIndexPath = [segcenter copy];
        }
            break;
        case 2:
        {
            carTypeTableView.hidden = YES;
            priceTableView.hidden = YES;
            servieTableView.hidden = NO;
            lastIndexPath = [segright copy];
        }
            break;

        default:
            break;
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == carTypeTableView) {
        self.contentArray = dataArray[0];
    }else if(tableView == priceTableView){
        self.contentArray = dataArray[1];
    }else{
        self.contentArray = dataArray[2];
    }
    return self.contentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    SelectCarTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[SelectCarTypeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSUInteger row = [indexPath row];
    NSUInteger oldRow = [lastIndexPath row];
    cell.checkView.image = (row == oldRow && lastIndexPath != nil) ?
    [UIImage imageNamed:@"datastaus_s"] : [UIImage imageNamed:@"datastatus_x"];

    cell.labelText.text = self.contentArray[indexPath.row];
    cell.checkView.image = [UIImage imageNamed:@"datastatus_x"];
    
    NSLog(@"%@",lastIndexPath);
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger newRow = [indexPath row];
    NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    
    if (newRow != oldRow)
    {
        SelectCarTypeTableViewCell *newCell = (SelectCarTypeTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
        newCell.checkView.image = [UIImage imageNamed:@"datastaus_s"];
        
        if (segment.selectedSegmentIndex == 0 || segment.selectedSegmentIndex == 1) {
            SelectCarTypeTableViewCell *oldCell = (SelectCarTypeTableViewCell *) [tableView cellForRowAtIndexPath:lastIndexPath];
            
            oldCell.checkView.image = [UIImage imageNamed:@"datastatus_x"];
        }
        lastIndexPath = [indexPath copy];
        
        if (segment.selectedSegmentIndex == 0) {
            self.carType = dataArray[0][indexPath.row];
        }else if (segment.selectedSegmentIndex == 1)
        {
            self.price = dataArray[1][indexPath.row];
        }else{
            [_service addObject:dataArray[2][indexPath.row]];
        }

        
    }else{
    
        SelectCarTypeTableViewCell *newCell = (SelectCarTypeTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
        newCell.checkView.image = [UIImage imageNamed:@"datastaus_s"];

        if (segment.selectedSegmentIndex == 0) {
            self.carType = @"";
        }else if (segment.selectedSegmentIndex == 1)
        {
            self.price = @"";
        }else{
            [_service removeObject:dataArray[2][indexPath.row]];
        }
        lastIndexPath = [indexPath copy];

    }
    
    if (segment.selectedSegmentIndex == 0) {
        segleft = [lastIndexPath copy];
    }if (segment.selectedSegmentIndex == 1) {
        segcenter = [lastIndexPath copy];
    }if (segment.selectedSegmentIndex == 2){
        segright = [lastIndexPath copy];
    }

    
}

- (void)viewWillDisappear:(BOOL)animated
{
    NSLog(@"按车型%@",self.carType);
    NSLog(@"按价格%@",self.price);
    
    for (int i = 0; i < _service.count; i++) {
        NSLog(@"%@",_service[i]);
    }
    
//    NSLog(@"按服务商%@",self.contentArray);
    
    NSString *json = [NSString stringWithFormat:@"\"cartype\":%@,\"price\":%@,\"service\":%@,",self.carType,self.price,self.service];
    NSLog(@"%@",json);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
